package fr.umlv.ig.metroView.map;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;

import fr.umlv.ig.model.MetroModel;
import fr.umlv.ig.model.Train;

/**
 * Represents a Train in the view.
 * @author jenseric
 *
 */
@SuppressWarnings("serial")
public class JTrain extends JComponent {
	/**train which represents the JTrain in the model */
	private final Train train;
	/**Mouse Adapter of the jtrain*/
	private final MouseAdapter mouseAdapter;
	
	/**Scale of the draw of the Jtrain*/
	private double scale = 1;
	
	/**Color of the JTrain*/
	private Color color= Color.WHITE;
	
	/**
	 * Initializes a {@link JTrain} with a {@link Train}
	 * @param train the train which represents the {@link JTrain} in a 
	 * {@link MetroModel}
	 */
	public JTrain(final Train train) {
		this.train = train;
		mouseAdapter = new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent e) {
				if(train == null) {
					return;
				}
				MetroMapTrains m = (MetroMapTrains)getParent();
				if(SwingUtilities.isLeftMouseButton(e)) {
					m.giveSelectedTrain(train);
				}
				if(SwingUtilities.isRightMouseButton(e)) {
					m.giveRemovedTrain(train);
				}
			}
		};
		addMouseListener(mouseAdapter);
		setBounds(getX(), getY(), 10, 10);
		setToolTipText("train id : " + train.getId());
		
	}
	
	/**
	 * Returns the train associated by the JTrain.
	 * @return the train associated by the JTrain.
	 */
	public Train getTrain() {
		return train;
	}
	
	@Override
	protected void paintComponent(Graphics g) {
		g.setColor(color);
		g.fillOval(0, 0,(int) (5.0 *scale),(int) (5.0 * scale));
	}
	
	/**
	 * Sets the scale of the component.
	 * @param scale the new scale of the compoenent.
	 */
	public void setDataTransforms(double scale) {
		this.scale = scale;
	}
	
	/**
	 * Returns the color of the JTrain.
	 * @return
	 */
	public Color getColor() {
		return color;
	}
	
	/**
	 * Sets the color of the JTrain.
	 * @param color
	 */
	public void setColor(Color color) {
		this.color = color;
	}

}
